<template>
	<div class="wrapper">

		<!-- header部分 -->
		<header>
			<p>修改商家信息</p>
		</header>

		<!-- 表单部分 -->
		<ul class="form-box">
			<li>
				<div class="title">
					商家名称：
				</div>
				<div class="content">
					<input type="text" v-model="business.businessName">
				</div>
			</li>
			<li>
				<div class="title">
					商家地址：
				</div>
				<div class="content">
					<input type="text" v-model="business.businessAddress">
				</div>
			</li>
			<li>
				<div class="title">
					商家说明：
				</div>
				<div class="content">
					<input type="text" v-model="business.businessExplain">
				</div>
			</li>
			<li>
				<div class="title">
					商家图片：
				</div>
				<div class="content">
					<input type="text" v-model="business.businessImg">
				</div>
			</li>
			<li>
				<div class="title">
					点餐分类：
				</div>
				<select v-model="business.orderTypeId">
					<option :value="1">美食</option>
					<option :value="2">早餐</option>
					<option :value="3">跑腿代购</option>
					<option :value="4">汉堡披萨</option>
					<option :value="5">甜品饮品</option>
					<option :value="6">速食简餐</option>
					<option :value="7">地方小吃</option>
					<option :value="8">米粉面馆</option>
					<option :value="9">包子粥铺</option>
					<option :value="10">炸鸡炸串</option>
				</select>
			</li>
			<li>
				<div class="title">
					起送费：
				</div>
				<div class="content">
					<input type="text" v-model="business.starPrice">
				</div>
			</li>
			<li>
				<div class="title">
					配送费：
				</div>
				<div class="content">
					<input type="text" v-model="business.deliveryPrice">
				</div>
			</li>
			
		</ul>

		<div class="button-login">
			<button @click="update()">保存</button>
		</div>

		<!-- 底部菜单部分 -->
		<Footer></Footer>
	</div>
</template>

<script setup>
import { ref, onMounted, getCurrentInstance } from 'vue';
import Footer from '../components/Footer.vue';
import axios from 'axios';
import qs from 'qs';
import { useRouter, useRoute } from 'vue-router';

const business = ref({
  businessName: '',
  businessAddress: '',
  businessExplain: '',
  businessImg: '',
  orderTypeId: '',
  starPrice: '',
  deliveryPrice: '',
});
const user = ref({});
const router = useRouter();
const route = useRoute();
const {proxy} =getCurrentInstance(); 
// 获取路由参数中的business信息，并将其转换为响应式数据

onMounted(async () => {
	user.value = proxy.$getSessionStorage('user');
	business.value = JSON.parse(route.query.business);
});

const update = async () => {
  let flag = 1;
  if (business.value.businessName == '') {
    alert('商家名称不能为空！');
    return;
  }
  const response = await axios.post('BusinessController/checkBusinessNameDuplicate', qs.stringify({
    businessName: business.value.businessName,
    businessId: business.value.businessId
  }));
  if (response.data == 1) {
    business.value.businessName = '';
    flag = 0;
    alert('此商家名称已存在！');
  }
  if (flag === 0) return;

  if (business.value.businessAddress == '') {
    alert('商家地址不能为空！');
    return;
  }
  if (business.value.businessExplain == '') {
    alert('商家说明不能为空！');
    return;
  }
  if (business.value.businessImg == '') {
    alert('商家图片不能为空！');
    return;
  }
  if (business.value.orderTypeId == '') {
    alert('点餐分类不能为空！');
    return;
  }
  if (business.value.starPrice == '') {
    alert('起送费不能为空！');
    return;
  }
  if (business.value.deliveryPrice == '') {
    alert('配送费不能为空！');
    return;
  }

  // 更新请求
  const responseUpdate = await axios.post('BusinessController/updateBusinessInfo', qs.stringify(business.value));
  if (responseUpdate.data > 0) {
    alert('修改成功！');
    router.go(-1);
  } else {
    alert('修改失败！');
  }
};
</script>

<style scoped>
	/****************** 总容器 ******************/
	.wrapper {
		width: 100%;
		height: 100%;
	}

	/****************** header部分 ******************/
	.wrapper header {
		width: 100%;
		height: 12vw;
		background-color: #0097FF;
		color: #fff;
		font-size: 4.8vw;

		position: fixed;
		left: 0;
		top: 0;
		z-index: 1000;

		display: flex;
		justify-content: center;
		align-items: center;
	}

	/****************** 表单部分 ******************/
	.wrapper .form-box {
		width: 100%;
		margin-top: 12vw;
	}

	.wrapper .form-box li {
		box-sizing: border-box;
		padding: 4vw 3vw 0 3vw;
		display: flex;
		align-items: center;
	}

	.wrapper .form-box li .title {
		flex: 0 0 18vw;
		font-size: 3vw;
		font-weight: 700;
		color: #666;
	}

	.wrapper .form-box li .content {
		flex: 1;
	}

	.wrapper .form-box li .content input {
		border: none;
		outline: none;
		width: 100%;
		height: 4vw;
		font-size: 3vw;
	}

	.wrapper .button-login {
		width: 100%;
		box-sizing: border-box;
		padding: 4vw 3vw 0 3vw;
	}

	.wrapper .button-login button {
		width: 100%;
		height: 10vw;
		font-size: 3.8vw;
		font-weight: 700;
		color: #fff;
		background-color: #38CA73;
		border-radius: 4px;

		border: none;
		outline: none;
	}

	.wrapper .button-register {
		width: 100%;
		box-sizing: border-box;
		padding: 4vw 3vw 0 3vw;
	}

	.wrapper .button-register button {
		width: 100%;
		height: 10vw;
		font-size: 3.8vw;
		font-weight: 700;
		color: #666;
		background-color: #EEE;
		border-radius: 4px;

		border: none;
		outline: none;
		border: solid 1px #DDD;
	}
</style>
